/**
 * @author yxw
 * date : 2013年11月30日 下午8:11:09 
 */
package com.tzinfo.tzt.activity.util;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Handler;
import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.tzinfo.tzt.util.NetworkGpsTools;

public class GPSThread {

	// 定位相关
	LocationClient mLocClient = null;
	static GPSThread gpsThread=null;
	public MyLocationListenner myListener = new MyLocationListenner();
	private String contextService = Context.LOCATION_SERVICE;
	boolean isRequestLocation = false;
	Context context;

	public GPSThread(Context context) {
		this.context = context;
		init();
	}
	
	public static GPSThread getInstance(Context context){
		if(gpsThread==null){
			gpsThread=new GPSThread(context);
		}
		return gpsThread;
	}

	Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			// Toast.makeText(HouseMapActivity.this, "msg:" + msg.what,
			// Toast.LENGTH_SHORT).show();
		};
	};

	public void requestLocation() {
		isRequestLocation = true;
		mLocClient.requestLocation();
		//双保险
		if(MyApplication.location == null){
			getLocation();
		}
	}

	
	public boolean getLocation() {
		// 通过系统服务，取得LocationManager对象
		LocationManager loctionManager = (LocationManager) context.getSystemService(contextService);
		if (!NetworkGpsTools.isOPenAllProvider(context)) {
			return false;
		}

		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);// 高精度
		criteria.setAltitudeRequired(false);// 不要求海拔
		criteria.setBearingRequired(false);// 不要求方位
		criteria.setCostAllowed(true);// 允许有花费
		criteria.setPowerRequirement(Criteria.POWER_LOW);// 低功耗
		// 从可用的位置提供器中，匹配以上标准的最佳提供器
		String provider = loctionManager.getBestProvider(criteria, true);
		// 获得最后一次变化的位置
		Location location = loctionManager.getLastKnownLocation(provider);
		if (location == null) {
			return false;
		}
		MyApplication.location = location;
		// Toast.makeText(
		// WelcomeActivity.this,
		// "经度：" + location.getLongitude() + "\n纬度："
		// + location.getLatitude(), 5000).show();
		return true;
	}
	private void init() {
		mLocClient = new LocationClient(context);
		mLocClient.registerLocationListener(myListener);
		LocationClientOption option = new LocationClientOption();


		option.setOpenGps(true);// 打开gps
		option.setCoorType("bd09ll"); // 设置坐标类型
		option.setScanSpan(50000);
		mLocClient.setLocOption(option);
		mLocClient.start();

	}

	/**
	 * 监听函数，又新位置的时候，格式化成字符串，输出到屏幕中
	 */
	public class MyLocationListenner implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null)
				return;
			
			if (isRequestLocation) {
				if (MyApplication.location == null) {
					Criteria criteria = new Criteria();
					LocationManager	locationManager=(LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
					String provider = locationManager.getBestProvider(criteria, true);
					if(provider==null){
						MyApplication.location = new Location("gps");
					}else{
						MyApplication.location = new Location(provider);
					}
					Log.d("location.getLatitude()","-->"+location.getLatitude());
					Log.d("location.getLongitude()","-->"+location.getLongitude());
					MyApplication.location.setLatitude(location.getLatitude());
					MyApplication.location.setLongitude(location.getLongitude());
				}else {
					Log.d("location.getLatitude()","-->"+location.getLatitude());
					Log.d("location.getLongitude()","-->"+location.getLongitude());
					MyApplication.location.setLatitude(location.getLatitude());
					MyApplication.location.setLongitude(location.getLongitude());
				}
				isRequestLocation = false;
			}

		}

		public void onReceivePoi(BDLocation poiLocation) {
			if (poiLocation == null) {
				return;
			}
		}
	}
}
